<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PCHIP interpolation</title>
<link rel="stylesheet" href="../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../interpolation.html" title="Chapter 13. Interpolation">
<link rel="prev" href="makima.html" title="Modified Akima interpolation">
<link rel="next" href="quintic_hermite.html" title="Quintic Hermite interpolation">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="makima.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interpolation.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="quintic_hermite.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.pchip"></a><a class="link" href="pchip.html" title="PCHIP interpolation">PCHIP interpolation</a>
</h2></div></div></div>
<h4>
<a name="math_toolkit.pchip.h0"></a>
      <span class="phrase"><a name="math_toolkit.pchip.synopsis"></a></span><a class="link" href="pchip.html#math_toolkit.pchip.synopsis">Synopsis</a>
    </h4>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">interpolators</span><span class="special">/</span><span class="identifier">pchip</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">interpolators</span> <span class="special">{</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RandomAccessContainer</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">pchip</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>

    <span class="keyword">using</span> <span class="identifier">Real</span> <span class="special">=</span> <span class="identifier">RandomAccessContainer</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">;</span>

    <span class="identifier">pchip</span><span class="special">(</span><span class="identifier">RandomAccessContainer</span><span class="special">&amp;&amp;</span> <span class="identifier">abscissas</span><span class="special">,</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;&amp;</span> <span class="identifier">ordinates</span><span class="special">,</span>
          <span class="identifier">Real</span> <span class="identifier">left_endpoint_derivative</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span>
          <span class="identifier">Real</span> <span class="identifier">right_endpoint_derivative</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">());</span>

    <span class="identifier">Real</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="identifier">Real</span> <span class="identifier">prime</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">push_back</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">y</span><span class="special">);</span>

    <span class="keyword">friend</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span> <span class="special">&amp;</span> <span class="identifier">os</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">pchip</span> <span class="special">&amp;</span> <span class="identifier">m</span><span class="special">);</span>
<span class="special">};</span>

<span class="special">}</span> <span class="comment">// namespaces</span>
</pre>
<h4>
<a name="math_toolkit.pchip.h1"></a>
      <span class="phrase"><a name="math_toolkit.pchip.pchip_interpolation"></a></span><a class="link" href="pchip.html#math_toolkit.pchip.pchip_interpolation">PCHIP
      Interpolation</a>
    </h4>
<p>
      The PCHIP interpolant takes non-equispaced data and interpolates between them
      via cubic Hermite polynomials whose slopes are chosen so that the resulting
      interpolant is monotonic; see <a href="https://doi.org/10.1137/0717021" target="_top">Fritsch
      and Carlson</a> for details. The interpolant is <span class="emphasis"><em>C</em></span><sup>1</sup> and
      evaluation has 𝑶(log(<span class="emphasis"><em>N</em></span>)) complexity. An example usage is
      as follows:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">{</span><span class="number">1</span><span class="special">,</span> <span class="number">5</span><span class="special">,</span> <span class="number">9</span> <span class="special">,</span> <span class="number">12</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">{</span><span class="number">8</span><span class="special">,</span><span class="number">17</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="special">-</span><span class="number">3</span><span class="special">};</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">interpolators</span><span class="special">::</span><span class="identifier">pchip</span><span class="special">;</span>
<span class="keyword">auto</span> <span class="identifier">spline</span> <span class="special">=</span> <span class="identifier">pchip</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">x</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">y</span><span class="special">));</span>
<span class="comment">// evaluate at a point:</span>
<span class="keyword">double</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">spline</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
<span class="comment">// evaluate derivative at a point:</span>
<span class="keyword">double</span> <span class="identifier">zprime</span> <span class="special">=</span> <span class="identifier">spline</span><span class="special">.</span><span class="identifier">prime</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
</pre>
<p>
      Periodically, it is helpful to see what data the interpolator has, and the
      slopes it has chosen. This can be achieved via
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">spline</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
</pre>
<p>
      Note that the interpolator is pimpl'd, so that copying the class is cheap,
      and hence it can be shared between threads. (The call operator and <code class="computeroutput"><span class="special">.</span><span class="identifier">prime</span><span class="special">()</span></code>
      are threadsafe; <code class="computeroutput"><span class="identifier">push_back</span></code> is
      not.)
    </p>
<p>
      This interpolant can be updated in constant time. Hence we can use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span></code>
      to do real-time interpolation:
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">circular_buffer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="special">...</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">initial_x</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">};</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">initial_y</span><span class="special">{</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">,</span><span class="number">6</span><span class="special">,</span><span class="number">7</span><span class="special">};</span>
<span class="keyword">auto</span> <span class="identifier">circular_pchip</span> <span class="special">=</span> <span class="identifier">pchip</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">initial_x</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">initial_y</span><span class="special">));</span>
<span class="comment">// interpolate via call operation:</span>
<span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">circular_pchip</span><span class="special">(</span><span class="number">3.5</span><span class="special">);</span>
<span class="comment">// add new data:</span>
<span class="identifier">circular_pchip</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">8</span><span class="special">);</span>
<span class="comment">// interpolate at 4.5:</span>
<span class="identifier">y</span> <span class="special">=</span> <span class="identifier">circular_pchip</span><span class="special">(</span><span class="number">4.5</span><span class="special">);</span>
</pre>
<p>
      <span class="inlinemediaobject"><object type="image/svg+xml" data="../../graphs/pchip.svg"></object></span>
    </p>
<h4>
<a name="math_toolkit.pchip.h2"></a>
      <span class="phrase"><a name="math_toolkit.pchip.complexity_and_performance"></a></span><a class="link" href="pchip.html#math_toolkit.pchip.complexity_and_performance">Complexity
      and Performance</a>
    </h4>
<p>
      This interpolator chooses the slopes and forwards data to the cubic Hermite
      interpolator, so the performance is stated in the documentation for <code class="computeroutput"><span class="identifier">cubic_hermite</span><span class="special">.</span><span class="identifier">hpp</span></code>.
    </p>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="makima.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interpolation.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="quintic_hermite.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
